package apps.softtech.fastmarket.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import apps.softtech.fastmarket.R;
import apps.softtech.fastmarket.adapter.AdapterSaleHistory.ViewHolder;
import apps.softtech.fastmarket.model.SaleTopic;

/**
 * 
 * @author MUI
 *
 */
public class AdapterPurchasedHistory extends BaseAdapter {

	private Context mContext;
	LayoutInflater inflater;
	private float rate=0;
	private String review="";
	private List<SaleTopic> listPurchase = new ArrayList<SaleTopic>();
	SaleTopic topic;
	
	public AdapterPurchasedHistory(Context context, List<SaleTopic> list ) {
		this.mContext = context;
		this.listPurchase = list;
		this.inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		
		if(listPurchase==null || listPurchase.equals(null) || listPurchase.size()==0){
			return 1;
		}
		return listPurchase.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = new ViewHolder();
		if(listPurchase==null || listPurchase.equals(null) || listPurchase.size()==0){
			convertView = inflater.inflate(R.layout.search_no_result, null);
			return convertView;
		}
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_history_purchased, null);
			initViewHolder(convertView, viewHolder);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		topic = listPurchase.get(position);
		
		viewHolder.tvNameTopic.setText(topic.getTopic());
		viewHolder.tvPrice.setText(String.valueOf(topic.getOfferPrice()));
		
		setEventButtonRate(viewHolder.btnRate);
		// do someting here
		return convertView;
	}
	
	private void setEventButtonRate(Button rate){
		rate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				displayDialogRate();
				
			}
		});
	}
	/**
	 * method display dialog to rate and review
	 */
	private void displayDialogRate(){
		
		final Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.dialog_rate_review);
		dialog.setTitle("Rate and Review");
		
		final RatingBar rating = (RatingBar) dialog.findViewById(R.id.rateBar);
		final EditText edtReview = (EditText) dialog.findViewById(R.id.edtReview);
		Button btnAccept = (Button) dialog.findViewById(R.id.btnAccept);
		final TextView tvAlert = (TextView) dialog.findViewById(R.id.tvAlert);
		
		
		btnAccept.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				rate = rating.getRating();
				review = edtReview.getText().toString();
				
				if(review.equals("") || review.equals(null)){
					Toast.makeText(mContext, String.valueOf(rate), Toast.LENGTH_SHORT).show();
					requestRateAndView();
					dialog.dismiss();
				}
				else if(!review.equals("") && rate<=0){
					tvAlert.setVisibility(View.VISIBLE);
					tvAlert.setText(mContext.getResources().getString(R.string.alert_review));
				}else{
					Toast.makeText(mContext, String.valueOf(rate), Toast.LENGTH_SHORT).show();
					dialog.dismiss();
				}
			}
		});
		
		dialog.show();
		
		
	}

	/**
	 * request to server to send rate and review product purchased
	 */
	private void requestRateAndView(){
		
	}
	/**
	 * initViewholder id
	 * @param v
	 * @param holder
	 */
	private void initViewHolder(View v, ViewHolder holder) {
		holder.btnRate = (Button) v.findViewById(R.id.btnRate);
		holder.tvNameTopic = (TextView) v.findViewById(R.id.tvNameTopic);
		holder.tvPrice = (TextView) v.findViewById(R.id.tvPrice);
	}

	static class ViewHolder {
		Button btnRate;
		TextView tvNameTopic;
		TextView tvPrice;
	}
	
	
}
